//
//  LBDoublePickerView.m
//  app
//
//  Created by wzl on 2017/4/10.
//  Copyright © 2017年 NAICAI LI. All rights reserved.
//

#import "LBDoublePickerView.h"
#import <Masonry/Masonry.h>
#define WINDOW         [[UIApplication sharedApplication].delegate window]
static CGFloat const LBSinglePickerViewCityPickerTopHeight = 35;

static CGFloat const LBSinglePickerViewCityPickerHeight = (216 + LBSinglePickerViewCityPickerTopHeight);
@interface LBDoublePickerView ()<UIPickerViewDelegate, UIPickerViewDataSource>

@property (strong, nonatomic) UIView *cityPickerBgView;

@property (copy, nonatomic) lbDoubleSelectedViewChoseBlock currentChoseBlock;

@property (strong, nonatomic) UIPickerView  *pickerView;

@property (assign, nonatomic) NSInteger yearSelectedIndex;

@property (assign, nonatomic) NSInteger monthSelectedIndex;

@property (strong, nonatomic) UILabel *showLabel;

@property (nonatomic ,strong) NSMutableArray *yearArray;

@property (nonatomic ,strong) NSMutableArray *monthArray;

@property (nonatomic ,strong) NSDictionary *dictionary;

@end

@implementation LBDoublePickerView

+ (instancetype)shareSinglePicker
{
    static LBDoublePickerView *shareDoublePicker = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        shareDoublePicker = [[LBDoublePickerView alloc]initWithFrame:WINDOW.bounds];
        [WINDOW addSubview:shareDoublePicker];
        [shareDoublePicker setHidden:YES];
    });
    return shareDoublePicker;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self createAllView];
    }
    return self;
}

- (void)createAllView
{
    [self addSubview:self.cityPickerBgView];
    
    [self addTarget:self action:@selector(closeCurrentView) forControlEvents:UIControlEventTouchUpInside];
    
    self.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0.4];
    
    self.yearSelectedIndex = 0;
    self.monthSelectedIndex = 0;
}

#pragma mark - getter

- (UIView *)cityPickerBgView
{
    if (!_cityPickerBgView) {
        _cityPickerBgView = [[UIView alloc]initWithFrame:CGRectMake(0, [UIScreen mainScreen].bounds.size.height, [UIScreen mainScreen].bounds.size.width, LBSinglePickerViewCityPickerHeight)];
        _cityPickerBgView.backgroundColor = [UIColor whiteColor];
        
        UIView *topView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width,LBSinglePickerViewCityPickerTopHeight)];
        
        topView.backgroundColor =  [UIColor colorWithRed:243/255.0 green:244/255.0 blue:245/255.0 alpha:1.0];
        
        UIButton *cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [cancelButton setTitle:@"取消" forState:UIControlStateNormal];
        
        [cancelButton setTitleColor:[UIColor colorWithRed:92/255.0 green:167/255.0 blue:240/255.0 alpha:1.0] forState:UIControlStateNormal];
        cancelButton.titleLabel.font = [UIFont systemFontOfSize:14];
        
        UILabel *titleLabel = [[UILabel alloc]init];
        [titleLabel setText:@"选择"];
        self.showLabel = titleLabel;
        titleLabel.font = [UIFont systemFontOfSize:15];
        titleLabel.textAlignment = NSTextAlignmentCenter;
        titleLabel.textColor = [UIColor lightGrayColor];
        
        UIButton *confirmButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [confirmButton setTitle:@"确定" forState:UIControlStateNormal];
        [confirmButton setTitleColor:[UIColor colorWithRed:92/255.0 green:167/255.0 blue:240/255.0 alpha:1.0] forState:UIControlStateNormal];
        confirmButton.titleLabel.font = [UIFont systemFontOfSize:14];
        
        [_cityPickerBgView addSubview:topView];
        
        [topView addSubview:cancelButton];
        [topView addSubview:titleLabel];
        [topView addSubview:confirmButton];
        
        [cancelButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(topView).offset(15);
            make.centerY.mas_equalTo(topView);
        }];
        
        [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.mas_equalTo(topView);
            make.centerY.mas_equalTo(topView);
        }];
        
        [confirmButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(topView).offset(-15);
            make.centerY.mas_equalTo(topView);
        }];
        
        [cancelButton addTarget:self action:@selector(cancelAction) forControlEvents:UIControlEventTouchUpInside];
        
        [confirmButton addTarget:self action:@selector(confirmActon) forControlEvents:UIControlEventTouchUpInside];
        
        [_cityPickerBgView addSubview:self.pickerView];
        
        CGRect newRect = _cityPickerBgView.bounds;
        
        newRect.origin.y = LBSinglePickerViewCityPickerTopHeight;
        
        newRect.size.height -= LBSinglePickerViewCityPickerTopHeight;
        
        [self.pickerView setFrame:newRect];
        
    }
    return _cityPickerBgView;
}

- (UIPickerView *)pickerView
{
    if (!_pickerView) {
        _pickerView = [[UIPickerView alloc] init];
        _pickerView.delegate = self;
        _pickerView.dataSource = self;
        _pickerView.backgroundColor = [UIColor whiteColor];
    }
    return _pickerView;
}

#pragma mark -action

- (void)cancelAction
{
    [self closeCurrentView];
}

- (void)confirmActon
{
    [self closeCurrentView];
    if (self.currentChoseBlock) {
        self.currentChoseBlock(self.yearSelectedIndex ,self.monthSelectedIndex);
    }
}

- (void)closeCurrentView
{
    __weak typeof(self) weakSelf = self;
    CGRect frame = self.cityPickerBgView.frame;
    frame.origin.y = [UIScreen mainScreen].bounds.size.height;
    [UIView animateWithDuration:0.25 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        weakSelf.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0];
        weakSelf.cityPickerBgView.frame = frame;
    } completion:^(BOOL finished) {
        [weakSelf setHidden:YES];
    }];
}


+ (void)showSinglePickViewByAnimation:(BOOL)animation
                            showTitle:(NSString *)showTitle
                            yearModel:(NSMutableArray *)yearModel
                           monthModel:(NSMutableArray *)monthModel
                             Complete:(lbDoubleSelectedViewChoseBlock)choseBlock{
    
    LBDoublePickerView *doublePickerView = [self shareSinglePicker];
    
    [WINDOW bringSubviewToFront:doublePickerView];
    
    [doublePickerView setHidden:NO];
    
    doublePickerView.currentChoseBlock = choseBlock;
    
    if (showTitle && showTitle.length) {
        [doublePickerView.showLabel setText:showTitle];
    } else {
        [doublePickerView.showLabel setText:@"选项"];
    }
    doublePickerView.yearArray = yearModel;
    doublePickerView.monthArray = monthModel;
    
    [doublePickerView.pickerView reloadAllComponents];
    
    if (yearModel.count && monthModel.count) {
        if (doublePickerView.yearSelectedIndex > doublePickerView.yearArray.count - 1) {
            doublePickerView.yearSelectedIndex = doublePickerView.yearArray.count - 1;
        }
        [doublePickerView.pickerView selectRow:doublePickerView.yearSelectedIndex inComponent:0 animated:YES];
        [doublePickerView.pickerView selectRow:doublePickerView.monthSelectedIndex inComponent:1 animated:YES];
//        doublePickerView.yearSelectedIndex = 0;
//        doublePickerView.monthSelectedIndex = 0;
    }
    
    if (doublePickerView.cityPickerBgView.frame.origin.y != [UIScreen mainScreen].bounds.size.height) {
        CGRect frame = doublePickerView.cityPickerBgView.frame;
        frame.origin.y = [UIScreen mainScreen].bounds.size.height;
        doublePickerView.cityPickerBgView.frame = frame;
    }
    doublePickerView.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0];

    [UIView animateWithDuration:0.25 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        doublePickerView.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0.4];
        CGRect frame = doublePickerView.cityPickerBgView.frame;
        frame.origin.y = [UIScreen mainScreen].bounds.size.height - LBSinglePickerViewCityPickerHeight;
        doublePickerView.cityPickerBgView.frame = frame;
    } completion:^(BOOL finished) {
        
    }];
}

#pragma mark - UIPickerViewDelegate, UIPickerViewDataSource

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 2;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    if (component == 0) {
        return self.yearArray.count;
    }else if (component == 1){
        return self.monthArray.count;
    }
    return 0;
}

- (nullable NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    if (row < self.yearArray.count && component == 0) {
        return self.yearArray[row];
    }else if (row < self.monthArray.count && component == 1){
        if ([self.yearArray[self.yearSelectedIndex] isEqualToString:@"至今"]) {
            return nil;
        }
        return self.monthArray[row];
    }
    return nil;
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    if (component == 0) {
        self.yearSelectedIndex = row;
        [pickerView reloadComponent:1];
    }else if (component == 1){
        self.monthSelectedIndex = row;
    }
    
}



@end
